package top.rish.universe.core.plugin;

import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.noear.snack.ONode;
import org.noear.socketd.transport.core.Session;
import org.noear.socketd.transport.core.entity.StringEntity;
import org.noear.solon.Solon;
import org.noear.solon.core.event.EventBus;
import org.noear.solon.core.util.LogUtil;

import java.io.File;
import java.io.IOException;

public class PluginClassFilesListener extends FileAlterationListenerAdaptor {
    UniversePluginEntity plugin;
    public PluginClassFilesListener(UniversePluginEntity uniPlugin) {
        this.plugin = uniPlugin;
    }

    private void pluginRestart(){
        if (this.plugin!=null) {
            this.plugin.restart();
            EventBus.publish(new PluginRestartEndEvent(this.plugin.getId()));

        }
    }


    @Override
    public void onDirectoryCreate(File directory) {
        this.pluginRestart();
    }

    @Override
    public void onDirectoryChange(File directory) {
        this.pluginRestart();
    }

    @Override
    public void onDirectoryDelete(File directory) {
        this.pluginRestart();
    }

    @Override
    public void onFileCreate(File file) {
        this.pluginRestart();
    }

    @Override
    public void onFileChange(File file) {
        this.pluginRestart();
    }

    @Override
    public void onFileDelete(File file) {
        this.pluginRestart();
    }


}
